<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
//require('echarts/theme/macarons'); // echarts theme
import resize from '../components/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '314px'
    },
    apiData: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      chart: null,
      option: {
        tooltip: {
          trigger: 'axis',
          formatter: '{b}<br />{a0}: {c0}批<br />{a1}: {c1}辆<br />{a2}: {c2}小时<br />{a3}: {c3}钩',
          axisPointer: {
            type: 'shadow',
            label: {
              backgroundColor: '#6a7985'
            }
          },
          textStyle: {
            color: '#fff',
            fontStyle: 'normal',
            fontFamily: '微软雅黑',
            fontSize: 12
          }
        },
        grid: {
          top: 35,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        legend: {
          data: ['调车批数', '车辆数', '作业时间', '调车钩数'],
          textStyle: {
            color: '#fff'
          }
        },
        color: ['#52BCA9', '#E46666', '#3BA272', '#FC8452', '#9A60B4', '#73C0DE', '#91CC75', '#5470C6'],
        xAxis: [
          {
            type: 'category',
            data: [],
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#fff'],
                opacity: 0.1
              }
            }
          }
        ],
        series: [
          {
            name: '调车批数',
            type: 'bar',
            barGap: '0',
            barWidth: 30,
            data: [],
            label: {
              //图形上的文本标签
              normal: {
                show: true,
                position: 'top',
                textStyle: {
                  color: '#a8aab0',
                  fontStyle: 'normal',
                  fontFamily: '微软雅黑',
                  fontSize: 12
                }
              }
            },
            itemStyle: {
              //图形样式
              normal: {
                barBorderRadius: [5, 5, 0, 0]
              }
            }
          },
          {
            name: '车辆数',
            type: 'bar',
            barGap: '0',
            barWidth: 30,
            data: [],
            label: {
              //图形上的文本标签
              normal: {
                show: true,
                position: 'top',
                textStyle: {
                  color: '#a8aab0',
                  fontStyle: 'normal',
                  fontFamily: '微软雅黑',
                  fontSize: 12
                }
              }
            },
            itemStyle: {
              //图形样式
              normal: {
                barBorderRadius: [5, 5, 0, 0]
              }
            }
          },
          {
            name: '作业时间',
            type: 'bar',
            barGap: '0',
            barWidth: 30,
            data: [],
            label: {
              //图形上的文本标签
              normal: {
                show: true,
                position: 'top',
                textStyle: {
                  color: '#a8aab0',
                  fontStyle: 'normal',
                  fontFamily: '微软雅黑',
                  fontSize: 12
                }
              }
            },
            itemStyle: {
              //图形样式
              normal: {
                barBorderRadius: [5, 5, 0, 0]
              }
            }
          },
          {
            name: '调车钩数',
            type: 'bar',
            barGap: '0',
            barWidth: 30,
            data: [],
            label: {
              //图形上的文本标签
              normal: {
                show: true,
                position: 'top',
                textStyle: {
                  color: '#a8aab0',
                  fontStyle: 'normal',
                  fontFamily: '微软雅黑',
                  fontSize: 12
                }
              }
            },
            itemStyle: {
              //图形样式
              normal: {
                barBorderRadius: [5, 5, 0, 0]
              }
            }
          }
        ]
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.option.xAxis[0].data = []
      for (var i = 0; i < 4; i++) {
        this.option.series[i].data = []
      }
      this.chart.setOption(this.option)
    },
    updateChart() {
      this.option.xAxis[0].data[0] = this.apiData.xAxisVal
      this.option.series[0].data[0] = this.apiData.batchNumber
      this.option.series[1].data[0] = this.apiData.clNumber
      this.option.series[2].data[0] = this.apiData.hour
      this.option.series[3].data[0] = this.apiData.hungNumber
      this.chart.setOption(this.option)
    }
  }
}
</script>
